package com.hxd.otc.module.my.presenter;


import android.support.annotation.NonNull;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseBean;
import com.hxd.otc.common.UserHelper;
import com.hxd.otc.module.my.bean.AdvertCoinTypeBean;
import com.hxd.otc.module.my.bean.BankCardBean;
import com.hxd.otc.module.my.bean.MyBondBean;
import com.hxd.otc.module.my.contract.AdManagementContract;
import com.hxd.otc.module.my.model.BankModel;
import com.hxd.otc.module.my.model.OtcAdvertModel;
import com.hxd.otc.mvp.base.BasePresenter;
import com.hxd.otc.net.BaseObserver;
import com.hxd.otc.utils.ResourceUtil;

import java.util.ArrayList;

/**
 * Created by lzy on 2018/6/11 0011.
 * description ${广告管理 Presenter}
 */
public class AdManagementPresenter extends BasePresenter<AdManagementContract.View> implements AdManagementContract.Presenter {


    private final BankModel mBankModel;
    private final OtcAdvertModel mOtcAdvertModel;
    private ArrayList<BankCardBean> mBankCardBeans;
    private ArrayList<AdvertCoinTypeBean> mAdvertCoinTypeBeans = new ArrayList<>();

    public AdManagementPresenter(AdManagementContract.View view) {
        super(view);
        mBankModel = BankModel.getInstance();
        mOtcAdvertModel = OtcAdvertModel.getInstance();
    }

    @Override
    public void requestBankCardList(final boolean showMessage) {
        mBankModel.getBankCardEasyList(new BaseObserver<ArrayList<BankCardBean>>() {
            @Override
            public void onNext(String msg, ArrayList<BankCardBean> bankCardBeans) {
                mBankCardBeans = bankCardBeans;
                if (showMessage)
                    mView.showToast(ResourceUtil.getString(R.string.get_fiat_info_success));
            }

            @Override
            public void onError(int errType, String errMessage) {
                if (showMessage)
                    mView.showToast(errMessage);
            }
        }, mView.<BaseBean<ArrayList<BankCardBean>>>bindAutoDispose());
    }

    @Override
    public void requestCoinTypeInfo(final boolean showErrMessage) {
        mOtcAdvertModel.getCoinType(new BaseObserver<ArrayList<AdvertCoinTypeBean>>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull ArrayList<AdvertCoinTypeBean> coinTypeBeans) {
                mAdvertCoinTypeBeans.clear();
                mAdvertCoinTypeBeans.addAll(coinTypeBeans);
                if (showErrMessage)
                    mView.showToast(ResourceUtil.getString(R.string.get_coin_type_info_success));
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {
                if (showErrMessage)
                    mView.showToast(errMessage);
            }
        }, mView.<BaseBean<ArrayList<AdvertCoinTypeBean>>>bindAutoDispose(), UserHelper.getUserInfo().getId());
    }


    @Override
    public ArrayList<BankCardBean> getBankCardBeans() {
        if (mBankCardBeans == null) {
            mView.showToast(ResourceUtil.getString(R.string.get_payment_collection_mode_fail_an_retry_get));
            requestBankCardList(true);
            return null;
        }
        if (mBankCardBeans.size() == 0) {
            mView.onNotSetCollectionMode(ResourceUtil.getString(R.string.not_set_the_payment_collection_mode));
            requestBankCardList(true);
            return null;
        }
        return mBankCardBeans;
    }

    @Override
    public ArrayList<AdvertCoinTypeBean> getAdvertCoinTypeBeans() {

        if (mAdvertCoinTypeBeans.size() == 0) {
            mView.showToast(ResourceUtil.getString(R.string.get_coin_info_fail_and_retry_get));
            requestCoinTypeInfo(true);
            return null;
        }

        return mAdvertCoinTypeBeans;
    }

    @Override
    public void getBond() {
        mOtcAdvertModel.getADBond(new BaseObserver<MyBondBean>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull MyBondBean bean) {
                mView.setBond(bean);
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {

                mView.showToast(errMessage);
            }
        }, mView.<BaseBean<MyBondBean>>bindAutoDispose());
    }

}
